package com.boot.controller;

import com.boot.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Controller
public class IndexController {

    @GetMapping("/index") //GetMapping就是RequestMapping+mehtod GET
    public String index(Model model,HttpServletRequest request, HttpSession session){
        //return ModelAndView
        //Model 当做参数
        //ModelMap 当做参数
        //Map 当做参数
        //request、session、application
        model.addAttribute("userName","张三");
        model.addAttribute("user",new User("张三","123456"));

        session.setAttribute("age",22);

        ServletContext app = request.getServletContext();

        app.setAttribute("sysConfig","系统配置");

        model.addAttribute("myDate",new Date());
        model.addAttribute("myImage","http://localhost:8001/02.png");
        List<User> users = Arrays.asList(
          new User("张三","123456"),
          new User("李四","123456"),
          new User("王五","123456"),
          new User("赵六","123456")
        );
        model.addAttribute("users", users);

        return "index";//返回视图名称
    }

    @GetMapping("/getUser")
    @ResponseBody
    public String getUser(Long id,String userName){
        System.out.println("id->"+id);
        System.out.println("userName->"+userName);
        return "user";
    }

    @GetMapping("/fragment_test")
    public String fragment_test(Model model){
        model.addAttribute("password","123456");
        return "fragment_test";
    }

}
